#include <iostream>
#include <string.h>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>
#include <stack>
#include <stdexcept>
#include <memory>
#include <unordered_map>

using namespace std;

typedef enum{Link, Thread} NodeTag;
#define MaxNum 99999999999;

class TreeNode
{
private:
    char data;              //值
    TreeNode* leftChild;    //左孩子
    TreeNode* rightChild;   //右孩子
    NodeTag lTag, rTag;     //线索Tag

    friend class MyTree;    //树类
public:
    TreeNode(char, TreeNode*, TreeNode*, NodeTag, NodeTag);
    ~TreeNode();

    void printNode();
    
};

class MyTree
{
private:
     TreeNode* root;        //根指针
     bool isThread;         //线索二叉树标志

public:
    MyTree();               //无参构造
    MyTree(const char[]);   //构造函数
    MyTree(const MyTree&);  //拷贝函数
    ~MyTree();              //析构函数

    void preOrderTraverse();    //前序遍历
    void inOrderTraverse();     //中序遍历
    void postOrderTraverse();   //后序遍历
    int countLeaf();            //叶结点计数
    int countHeight();          //计算树的深度
    bool isThreadedTree();      //判断是否为线索二叉树
    bool inOrderThreading();    //线索化
    TreeNode& locateNode(const char&);  //结点定位
    TreeNode& preNode(const TreeNode&); //获取前驱结点
    TreeNode& nextNode(const TreeNode&);//获取后继结点

     TreeNode* createTree(const char[],int*);
    TreeNode* copyTree(TreeNode*);
    void DeleteTree(TreeNode*);
    void preOrder(TreeNode*);
    void inOrder(TreeNode*);
    void inOrderTraverse_Thr(TreeNode*);
    void postOrder(TreeNode*);

    int leafNum(TreeNode*);
    int treeHeight(TreeNode*);
    void inThreading(TreeNode*);
    TreeNode* locate(TreeNode* , const char&);
    TreeNode* inOrderLastNode(TreeNode*);
    TreeNode* inOrderFirstNode(TreeNode*);

};




class HuffmanNode {
public:
    int value;
    int freq;
    HuffmanNode* left;
    HuffmanNode* right;

    HuffmanNode(int v, int f);
};

struct Compare {
    bool operator()(HuffmanNode* a, HuffmanNode* b);
};

class HuffmanTree
{
private:
    //添加必要的数据成员
  HuffmanNode* root;

   void generateCodes(HuffmanNode* node, string code, map<int, string>& codes);

public:
    HuffmanTree(const int&,const int[]);    //构造函数
    ~HuffmanTree();     //析构函数

    //输出哈夫曼编码
    //格式为：每行输出一个节点，节点排序递减
    //节点1值：编码
    //节点2值：编码
    //....
    //cpp中未给出该函数，遵守输出格式规范
    void printHuffmanCodes();   

 
};


